﻿using System;
using Microsoft.SharePoint;

namespace DE.Sdc.SharePoint.Common.Deployment.Administration
{
    public sealed class AutomaticScheduling
    {
        /// <summary>
        /// default constructor
        /// </summary>
        public AutomaticScheduling()
        {
            
        }

        /// <summary>
        /// Creates a job schedule by a given period, start- and endtime
        /// </summary>
        /// <param name="automaticSchedulingDuration">Schedule period</param>
        /// <param name="automaticSchedulingStart">Schedule starttime depending on schedule period</param>
        /// <param name="automaticSchedulingEnd">Schedule endtime depending on schedule period</param>
        /// <returns>Job schedule</returns>
        public static SPSchedule CreateJobSchedule(Period automaticSchedulingDuration, int automaticSchedulingStart, int automaticSchedulingEnd)
        {
            SPSchedule schedule;

            switch (automaticSchedulingDuration)
            {
                //case Period.Minute:
                //    schedule = AutomaticScheduling.CreateMinuteSchedule(0, 59, 5);
                //    break;
                case Period.Hourly:
                    if (automaticSchedulingStart > 0 && automaticSchedulingEnd > 0)
                        schedule = CreateHourlySchedule(automaticSchedulingStart, automaticSchedulingEnd);
                    else
                        schedule = CreateHourlySchedule(0, 5);
                    break;
                case Period.Daily:
                    if (automaticSchedulingStart > 0 && automaticSchedulingEnd > 0)
                        schedule = CreateDailySchedule(automaticSchedulingStart, automaticSchedulingEnd, 0, 0, 0, 0);
                    else
                        schedule = CreateDailySchedule(3, 4, 0, 0, 0, 0);
                    break;
                case Period.Weekly:
                    if (automaticSchedulingStart > 0 && automaticSchedulingEnd > 0)
                    {
                        DayOfWeek beginDayOfWeek;
                        DayOfWeek endDayOfWeek;
                        if (automaticSchedulingStart == 7)
                            beginDayOfWeek = (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), 0);
                        else
                            beginDayOfWeek = (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), automaticSchedulingStart - 1);

                        if (automaticSchedulingEnd == 7)
                            endDayOfWeek = (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), 0);
                        else
                            endDayOfWeek = (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), automaticSchedulingEnd - 1);

                        schedule = CreateWeeklySchedule(beginDayOfWeek, endDayOfWeek, 3, 4, 0, 0, 0, 0);
                    }
                    else
                    {
                        DayOfWeek beginDayOfWeek = (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), 1);
                        DayOfWeek endDayOfWeek = (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), 0);
                        schedule = CreateWeeklySchedule(beginDayOfWeek, endDayOfWeek, 3, 4, 0, 0, 0, 0);
                    }
                    break;
                case Period.Monthly:
                    if (automaticSchedulingStart > 0 && automaticSchedulingEnd > 0)
                        schedule = CreateMonthlySchedule(automaticSchedulingStart, automaticSchedulingEnd, 3, 4, 0, 0, 0, 0);
                    else
                        schedule = CreateMonthlySchedule(1, 1, 3, 4, 0, 0, 0, 0);
                    break;
                case Period.Yearly:
                    if (automaticSchedulingStart > 0 && automaticSchedulingEnd > 0)
                        schedule = CreateYearlySchedule(automaticSchedulingStart, automaticSchedulingEnd, 1, 1, 10, 12, 0, 0, 0, 0);
                    else
                        schedule = CreateYearlySchedule(1, 1, 1, 1, 10, 12, 0, 0, 0, 0);
                    break;
                default:
                    schedule = null;
                    break;
            }

            return schedule;
        }

        /// <summary>
        /// Creates a Job Schedule that runs in a minute interval between a start and end second
        /// </summary>
        /// <param name="beginSecond">Start second of the interval</param>
        /// <param name="endSecond">End second of the interval</param>
        /// <param name="interval">Number of minutes in which the schedule will be executed</param>
        /// <returns>Job Minute Schedule</returns>
        public static SPMinuteSchedule CreateMinuteSchedule(int beginSecond, int endSecond, int interval)
        {
            SPMinuteSchedule minuteSchedule = new SPMinuteSchedule();
            minuteSchedule.BeginSecond = beginSecond;
            minuteSchedule.EndSecond = endSecond;
            minuteSchedule.Interval = interval;
            return minuteSchedule;
        }

        /// <summary>
        /// Creates a Job Schedule that every hour between a start and end minute
        /// </summary>
        /// <param name="beginMinute">Start minute of the interval</param>
        /// <param name="endMinute">End minute of the interval</param>
        /// <returns>Job Hourly Schedule</returns>
        public static SPHourlySchedule CreateHourlySchedule(int beginMinute, int endMinute)
        {
            SPHourlySchedule hourlySchedule = new SPHourlySchedule();
            hourlySchedule.BeginMinute = beginMinute;
            hourlySchedule.EndMinute = endMinute;
            return hourlySchedule;
        }

        /// <summary>
        /// Creates a Job Schedule that every day between a start and end time
        /// </summary>
        /// <param name="beginHour">Start hour of the interval</param>
        /// <param name="endHour">End hour of the interval</param>
        /// <param name="beginMinute">Start minute of the interval</param>
        /// <param name="endMinute">End minute of the interval</param>
        /// <param name="beginSecond">Start second of the interval</param>
        /// <param name="endSecond">End second of the interval</param>
        /// <returns>Job Daily Schedule</returns>
        public static SPDailySchedule CreateDailySchedule(int beginHour, int endHour, int beginMinute, int endMinute, int beginSecond, int endSecond)
        {
            SPDailySchedule dailySchedule = new SPDailySchedule();
            dailySchedule.BeginHour = beginHour;
            dailySchedule.BeginMinute = beginMinute;
            dailySchedule.BeginSecond = beginSecond;
            dailySchedule.EndHour = endHour;
            dailySchedule.EndMinute = endMinute;
            dailySchedule.EndSecond = endSecond;

            return dailySchedule;
        }

        /// <summary>
        /// Creates a Job Schedule that every week between a start and end time
        /// </summary>
        /// <param name="beginDayOfWeek">Start weekday of the interval</param>
        /// <param name="endDayOfWeek">End weekday of the interval</param>
        /// <param name="beginHour">Start hour of the interval</param>
        /// <param name="endHour">End hour of the interval</param>
        /// <param name="beginMinute">Start minute of the interval</param>
        /// <param name="endMinute">End minute of the interval</param>
        /// <param name="beginSecond">Start second of the interval</param>
        /// <param name="endSecond">End second of the interval</param>
        /// <returns>Job Weekly Schedule</returns>
        public static SPWeeklySchedule CreateWeeklySchedule(DayOfWeek beginDayOfWeek, DayOfWeek endDayOfWeek, int beginHour, int endHour, int beginMinute, int endMinute, int beginSecond, int endSecond)
        {
            SPWeeklySchedule weeklySchedule = new SPWeeklySchedule();
            weeklySchedule.BeginDayOfWeek = beginDayOfWeek;
            weeklySchedule.BeginHour = beginHour;
            weeklySchedule.BeginMinute = beginMinute;
            weeklySchedule.BeginSecond = beginSecond;
            weeklySchedule.EndDayOfWeek = endDayOfWeek;
            weeklySchedule.EndHour = endHour;
            weeklySchedule.EndMinute = endMinute;
            weeklySchedule.EndSecond = endSecond;
            return weeklySchedule;
        }

        /// <summary>
        /// Creates a Job Schedule that every month between a start and end time
        /// </summary>
        /// <param name="beginDay">Start day of the interval</param>
        /// <param name="endDay">End day of the interval</param>
        /// <param name="beginHour">Start hour of the interval</param>
        /// <param name="endHour">End hour of the interval</param>
        /// <param name="beginMinute">Start minute of the interval</param>
        /// <param name="endMinute">End minute of the interval</param>
        /// <param name="beginSecond">Start second of the interval</param>
        /// <param name="endSecond">End second of the interval</param>
        /// <returns>Job Monthly Schedule</returns>
        public static SPMonthlySchedule CreateMonthlySchedule(int beginDay, int endDay, int beginHour, int endHour, int beginMinute, int endMinute, int beginSecond, int endSecond)
        {
            SPMonthlySchedule monthlySchedule = new SPMonthlySchedule();
            monthlySchedule.BeginDay = beginDay;
            monthlySchedule.BeginHour = beginHour;
            monthlySchedule.BeginMinute = beginMinute;
            monthlySchedule.BeginSecond = beginSecond;
            monthlySchedule.EndDay = endDay;
            monthlySchedule.EndHour = endHour;
            monthlySchedule.EndMinute = endMinute;
            monthlySchedule.EndSecond = endSecond;
            return monthlySchedule;
        }

        /// <summary>
        /// Creates a Job Schedule that every year between a start and end time
        /// </summary>
        /// <param name="beginMonth">Start month of the interval</param>
        /// <param name="endMonth">End month of the interval</param>
        /// <param name="beginDay">Start day of the interval</param>
        /// <param name="endDay">End day of the interval</param>
        /// <param name="beginHour">Start hour of the interval</param>
        /// <param name="endHour">End hour of the interval</param>
        /// <param name="beginMinute">Start minute of the interval</param>
        /// <param name="endMinute">End minute of the interval</param>
        /// <param name="beginSecond">Start second of the interval</param>
        /// <param name="endSecond">End second of the interval</param>
        /// <returns>Job Yearly Schedule</returns>
        public static SPYearlySchedule CreateYearlySchedule(int beginMonth, int endMonth, int beginDay, int endDay, int beginHour, int endHour, int beginMinute, int endMinute, int beginSecond, int endSecond)
        {
            SPYearlySchedule yearlySchedule = new SPYearlySchedule();
            yearlySchedule.BeginMonth = beginMonth;
            yearlySchedule.BeginDay = beginDay;
            yearlySchedule.BeginHour = beginHour;
            yearlySchedule.BeginMinute = beginMinute;
            yearlySchedule.BeginSecond = beginSecond;
            yearlySchedule.EndMonth = endMonth;
            yearlySchedule.EndDay = endDay;
            yearlySchedule.EndHour = endHour;
            yearlySchedule.EndMinute = endMinute;
            yearlySchedule.EndSecond = endSecond;
            return yearlySchedule;
        }

    }
}
